package com.itheima.redbaby.engine.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.engine.BaseEngine;
import com.itheima.redbaby.engine.SearchEngine;

public class SearchEngineImpl extends BaseEngine implements SearchEngine {
	
	@Override
	public List<String> getRecommendSearch() {
		JSONObject json = getJson(TYPE_GET, ContantValue.REDBOY_RECOMMEND_SEARCH, null);
		List<String> list_search = JSONObject.parseArray(json.getString("search_keywords"), String.class);
		return list_search;
	}

	@Override
	public Map<String, Object> getProductList(String keyword, int page,
			int pageNum, String orderby) {
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("keyword", keyword);
		map.put("page", page);
		map.put("pageNum", pageNum);
		map.put("orderby", orderby);
		
		JSONObject json = getJson(TYPE_POST, ContantValue.REDBOY_SEARCH_URI, map);
		
		int count = json.getInteger("list_count");
		List<Product> list_product = JSONObject.parseArray(json.getString("productlist"), Product.class);
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("count", count);
		result.put("list_product", list_product);
		
		return result;
	}
	
}
